let OSS = require('ali-oss');
const crypto = require('crypto');

const accessKeyId = 'accessKeyId';
const secretAccessKey = 'secretAccessKey';
const region = 'region';
const bucket = 'bucket';

const client = new OSS({
  // yourRegion填写Bucket所在地域。以华东1（杭州）为例，Region填写为oss-cn-hangzhou。
  region: region,
  // 阿里云账号AccessKey拥有所有API的访问权限，风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维，请登录RAM控制台创建RAM用户。
  accessKeyId: accessKeyId,
  accessKeySecret: secretAccessKey,
  // 填写Bucket名称。
  bucket: bucket
});

const sign = function(){
	
	var configOSS = {
      dirPath: dirPath, // 存放到哪个目录下
      bucket: bucket, //bucketName
      host: `https://${bucket}.${region}.aliyuncs.com`, //自己的地址
      accessKeyId: accessKeyId, //自己的key
      accessKeySecret: secretAccessKey, //自己的KeySecret
      expAfter: 3000000, // 签名失效时间，毫秒
      maxSize: 1048576000 // 文件最大的 size
    }
    const expireTime = new Date().getTime() + configOSS.expAfter
    const expiration = new Date(expireTime).toISOString()

    const jasonCallback = JSON.stringify({
      callbackUrl: config.web.url + callbackPath,
      callbackBody: "fileName=${object}&size=${size}&mimeType=${mimeType}&height=${imageInfo.height}&width=${imageInfo.width}",
      //callbackBodyType:"application/json",
    })
    console.log('jasonCallback',jasonCallback)
    const callback = Buffer.from(jasonCallback).toString('base64');
    const policyString = JSON.stringify({
      expiration,
      conditions: [
        ['content-length-range', 0, configOSS.maxSize],
        ['starts-with', '$key', configOSS.dirPath],
      ],
    })
    const policy = Buffer.from(policyString).toString('base64');
    const signature = crypto.createHmac('sha1', configOSS.accessKeySecret).update(policy).digest("base64");
    return {
      'key': configOSS.dirPath ,
      policy,
      'OSSAccessKeyId': configOSS.accessKeyId,
      'success_action_status': 200,
      signature,
      'host': configOSS.host,
      'dirPath': configOSS.dirPath,
      callback
    }
}

module.exports = sign;